package top.chmin.project.cloud.common.column.code.auto;

// field import auto

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.column.code.api.ColumnCodeSelectByFieldCode;
import top.chmin.project.cloud.common.column.code.api.ColumnCodeUpdateByFieldCode;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.tool.LocalTool;

import java.time.LocalDateTime;


/**
 * chmin
 * 2023-12-27 19:40:30
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ColumnCodeService {

    @Resource
    ColumnCodePlugin plugin;

    @Resource
    ColumnCodeDao columnCodeDao;

// field labelDao auto

    public int save(ColumnCodeSave.Param param) {
        plugin.checkSave(param);
        ColumnCode columnCode = BeanUtil.toBean(param, ColumnCode.class);
        plugin.beforeSave(columnCode);
        int result = columnCodeDao.save(columnCode);
        plugin.afterSave(param, columnCode, result);
        return result;
    }

    public int updateById(ColumnCodeUpdateById.Param param) {
        plugin.checkUpdateById(param);
        ColumnCode columnCode = BeanUtil.toBean(param, ColumnCode.class);
        plugin.beforeUpdateById(columnCode);
        int result = columnCodeDao.updateById(columnCode);
        plugin.afterUpdateById(param, columnCode, result);
        return result;
    }

    public int deleteById(ColumnCodeDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = columnCodeDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public ColumnCodeSelectById.Result selectById(ColumnCodeSelectById.Param param) {
        plugin.checkSelectById(param);
        ColumnCodeFPC fpc = new ColumnCodeFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        ColumnCodeSelectById.Result result = columnCodeDao.getFirst(fpc.toCondition(), ColumnCodeSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<ColumnCodeSelectPage.Result> selectPage(ColumnCodeSelectPage.Param param) {
        plugin.checkSelectPage(param);
        ColumnCodeFPC fpc = new ColumnCodeFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<ColumnCodeSelectPage.Result> result = columnCodeDao.getPageByCondition(fpc.toCondition(), ColumnCodeSelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

    public ColumnCodeSelectByFieldCode.Result selectByFieldCode(ColumnCodeSelectByFieldCode.Param param) {
        return columnCodeDao.getFirst(
                new ColumnCodeFPC()
                        .fieldCode().eq(param.getFieldCode())
                        .toCondition(),
                ColumnCodeSelectByFieldCode.Result.class
        );
    }

    public void updateByFieldCode(ColumnCodeUpdateByFieldCode.Param param) {
        ColumnCode columnCode = BeanUtil.toBean(param, ColumnCode.class);
        columnCode.setUpdateBy(LocalTool.getUserId());
        columnCode.setUpdateTime(LocalDateTime.now());
        columnCodeDao.updateByCondition(
                columnCode.getPreCondition()
                        .forUpdate()
                        .fieldCode().eq()
                        .toCondition()
        );
    }

// entity select auto

// entity search auto

}
